<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.js"></script>
    <style>
        .box{
            width: 100px;
            height: 2000px;
            background-color: blue;
        }
    </style>
</head>
<body>
    <div class="box"></div>
    <button>回到顶部</button>
    <script>

        $("button").click(function(){
            // $(window).scrollTop(0);
          var timer =   setInterval(function(){
                console.log(111);
                var topNum = $(window).scrollTop();
                if(topNum<=0){
                    // 让定时器停止 ，让动画停止
                    clearInterval(timer);
                }
                topNum -= 20;
                $(window).scrollTop(topNum);
            }, 2);
        })

    </script>
</body>
</html>